// 搜索状态管理
const searchModule = ({
    state: {
        keywords: ["运动鞋", "高跟鞋", "帆布鞋", "平底鞋", "拖鞋", "马丁靴", "皮鞋", "裙子", "衬衫", "婚纱", "水果"],
        recommand: [],//提示区
        // history: JSON.parse(localStorage.history) || []//搜索历史
        history: []//搜索历史
    },
    mutations: {
        CHANGE_HISTORY(state, key) {
            //去重 
            var index = state.history.findIndex((element) => {
                return element === key
            });
            //删除原历史
            if (index !== -1) {
                state.history.splice(index, 1);
            }
            //添加历史至第一个
            state.history.unshift(key);
            //搜索历史长度限制
            if (state.history.length > 3) {
                state.history.pop();
            }
            //本地
            // localStorage.history = JSON.stringify(state.history);
        },
        FILTER_KEYWORDS(state, key) {
            //先清空搜索提示
            state.recommand = [];
            //查找关键词
            state.keywords.forEach((element) => {
                if (element.includes(key)) {
                    //符合即添加至提示区
                    state.recommand.push(element)
                    //   console.log(state.recommand)
                }
            })
        },
        DEL_RECOMMAND(state) {
            //删除搜索历史
            state.history = []
            //本地
            // localStorage.history = JSON.stringify(state.history);
        }
    },
    actions: {
        FILTER_KEYWORDS(context, key) {
            //添加搜索历史
            context.commit("FILTER_KEYWORDS", key)
        },
        CHANGE_HISTORY(context, key) {
            //提示区内容
            context.commit("CHANGE_HISTORY", key)
        },
        DEL_RECOMMAND(context) {
            //删除搜索历史
            context.commit("DEL_RECOMMAND")
        }
    },
    modules: {
    }
})

export default searchModule
